package com.xueyu.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.dao.ReceptionDao;
import com.xueyu.domain.Reception.ReceptionBean;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.text.SimpleDateFormat;
import java.util.*;
@RestController
@RequestMapping("/Reception")
//接待登记
public class ReceptionController {
    @Autowired
    ReceptionDao ReceptionDao;
    @RequestMapping(value = "/add")
    public ResultBean add(@RequestBody ReceptionBean ReceptionBean) throws Exception{
        ResultBean resultBean = new ResultBean();
         if(ReceptionBean.getManualRecordId()==0) {
             Date date = new Date();
             SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
             String currentTime = sdf.format(date);
             ReceptionBean.setVisitTime(currentTime);
             ReceptionDao.insert(ReceptionBean);
        }else {
             ReceptionDao.updateById(ReceptionBean);
        }
        return resultBean;
    }
    //删除
    @RequestMapping("/del/{manualRecordId}")
    public ResultBean del(@PathVariable("manualRecordId") int manualRecordId){
        ReceptionDao.deleteById(manualRecordId);
        return new ResultBean();
    }
//
//
//
    @RequestMapping("/getid/{manualRecordId}")
    public Map<String, Object> getid(@PathVariable("manualRecordId") int manualRecordId){
        ResultBean resultBean = new ResultBean();
        if (manualRecordId>0) {
            QueryWrapper<ReceptionBean> queryWrapper = Wrappers.query();
            queryWrapper.eq("manualRecordId", manualRecordId);
            List<ReceptionBean> ReceptionList = ReceptionDao.selectList(queryWrapper);
            resultBean.put("data", ReceptionList);
        }
        return resultBean;
    }


    //分页.条件查询.
    @RequestMapping("/fy")
    public ResultBean fy( @RequestBody  Map map) {
        int current = (Integer)map.get("Current");
        int size = (Integer)map.get("size");
        Page<ReceptionBean> page = new Page<>(current, size);
        QueryWrapper<ReceptionBean> queryWrapper = createSchoolQueryWrapper(map);
        page = ReceptionDao.selectPage(page, queryWrapper);
        List<ReceptionBean> list = page.getRecords();
        ResultBean resultBean = new ResultBean();
        resultBean.put("page",page);
        resultBean.put("list",list);
        return resultBean;
    }


//
    @RequestMapping("/zd")
    public ResultBean zd(@RequestBody  Map map)
    {
        QueryWrapper<ReceptionBean> queryWrapper = createSchoolQueryWrapper(map);
        List<ReceptionBean> list = ReceptionDao.selectList(queryWrapper);
        ResultBean resultBean = new ResultBean();
        resultBean.put("list",list);
        return resultBean;
    }
//
    //封装好的查询条件
    public QueryWrapper<ReceptionBean> createSchoolQueryWrapper(Map map) {
        String manualRecordId =  (String)map.get("manualRecordId");
        String visitor =  (String)map.get("visitor");
        String visitReason =  (String)map.get("visitReason");
        QueryWrapper<ReceptionBean> queryWrapper = Wrappers.query();
        if (manualRecordId != null && !"".equals(manualRecordId)) {
            queryWrapper.eq("manualRecordId", manualRecordId);
        }
        if (visitor != null && !"".equals(visitor)) {
            queryWrapper.like("visitor", "%" + visitor + "%");
        }
        if (visitReason != null && !"".equals(visitReason)) {
            queryWrapper.like("visitReason", "%" + visitReason + "%");
        }
        return queryWrapper;
    }
}




